home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / rexx / tex-server.rexx < prev    next >
OS/2 REXX Batch file  |  1995-03-15  |  8KB  |  296 lines

  1. /*RX
  2.  * AREXX    NAME:TeX-server.rexx    Version:1.42    DATE:30-Jul-91
  3.  *
  4.  This script creates an ARex port, by the name of
  5.  *
  6.  *    Port Name = "Start_TeX"
  7.  *
  8.  * and waits there for the commands
  9.  *
  10.  *    - compile <format-file> <TeX-file including-absolute-path>
  11.  *        - the filename *must* have an extension.
  12.  *        - if <format-file>=='&', use tex'es default format.
  13.  *    - quit
  14.  *        exits the server.
  15.  *
  16.  * It may be called with one parameter: the name of the public screen
  17.  * (OS 2.0) in which this server runs.
  18.  *
  19. AUTHORS:
  20.  *             April 91        J\"org H\"ohle
  21.  *        before        Georg Hessmann
  22. NOTES:
  23.  An `absolute path' is given if you use a volume, device or assign'ed
  24. name like TeX_Disk1:, RAM: or TeX:.
  25.  *
  26.  * Make virtex resident (ARP or AmigaDOS, depending on your shell), or
  27. include it in your shell path. However the default of 'virtex' may be
  28. overridden at any time by the ARexx variable virtex (lowercase!) (see
  29. further below). This has been implemented to be able to switch
  30. between smallTeX and BigTeX on the fly.
  31.  *
  32. BUGS:
  33.  Does not allow blanks in filenames and directories, because neither
  34. ARexx nor TeX are happy with them.
  35.  *
  36. FILES:
  37.  Rexx:namestruc        : namestruc is a function, and must stay in REXX:
  38.                 for now. Sorry about that.
  39.  Libs:rexxsupport.library
  40.  *
  41.  */
  42.  
  43. portname = "Start_TeX"        /* PortName */
  44.  
  45. /* Failat 10 */
  46. OPTIONS RESULTS
  47.  
  48. PARSE ARG PubScreen
  49. IF PubScreen = "" THEN PubScreen = "Workbench"
  50.  
  51. IF ~show('L','rexxsupport.library') THEN
  52.     IF ~addlib('rexxsupport.library',0,-30,0) THEN DO
  53.         say "Can't open 'rexxsupport.library'!"
  54.         EXIT 20
  55.         END
  56.  
  57. /* ARexx V1.10 sets RC to Result * 65536 + Result2
  58.      V1.14 and 1.15 set RC to Result    on a shell command call.
  59. */
  60. parse version . rexxver .
  61. filter2 = rexxver < "V1.14"
  62. /*say 'ver'rexxver 'filter 'filter2*/
  63.  
  64. SIGNAL ON break_C
  65. SIGNAL ON break_D
  66.  
  67. FALSE = 0
  68. TRUE = 1
  69.  
  70. say
  71. say "*************************************************"
  72. say "*    Welcome to the ARexx - PasTeX Interface    *"
  73. say "*************************************************"
  74. say
  75.  
  76. oldedit = upper(mygetenv(TEXREXX))
  77. /* default is no change, logical ? */
  78. IF "EDIT" = oldedit THEN DO
  79.     OPTIONS PROMPT "Call editor automatically on first error (Y/n)? "
  80.     PARSE UPPER PULL yn .
  81.     IF LEFT(yn,1) = "N"    THEN newedit = "e"
  82.                 ELSE newedit = "edit"
  83.     END
  84. ELSE DO
  85.     OPTIONS PROMPT "Call editor automatically on first error (y/N)? "
  86.     PARSE UPPER PULL yn .
  87.     IF LEFT(yn,1) = "Y"    THEN newedit = "edit"
  88.                 ELSE newedit = "e"
  89.     END
  90.  
  91. IF UPPER(newedit) ~= oldedit THEN CALL mysetenv(TEXREXX,newedit)
  92. /* if it were undefined, virtex would call ENV:EDITOR.
  93.      We want Rexx scripts to be used (TeXEdit.rexx). */
  94. DROP newedit oldedit
  95.  
  96. say
  97. OPTIONS PROMPT "Ask for format when default requested (y/N) ? "
  98. PARSE UPPER PULL yn .
  99. IF (LEFT(yn,1) = "Y") THEN
  100.   CALL SETCLIP("TEXQUERY","TRUE")
  101. ELSE
  102.   CALL SETCLIP("TEXQUERY")
  103.  
  104. OPTIONS PROMPT "? >"
  105. DROP yn
  106.  
  107. say
  108. say "- Opening port "portname"."
  109.  
  110. IF ~openport(portname) THEN DO
  111.   say
  112.   say "- Can't open it !"
  113.   EXIT 20
  114. END
  115.  
  116.  
  117. empty = FALSE
  118.  
  119. mainloop:
  120. DO UNTIL com = 'quit'
  121.  
  122.   IF ~empty THEN DO
  123.     CALL SETCLIP("TEXTIME")
  124.     say "- Waiting for command (compile/quit) on port "portname"."
  125.   END
  126.  
  127.   CALL waitpkt(portname)
  128.  
  129.   packet = getpkt(portname)
  130.   IF (packet == NULL()) THEN empty = TRUE /* say "- Received NULL packet." */
  131.           /* ARexx 1.14 and 1.15 tend to send *lots* of NULL packets */
  132.   ELSE DO
  133.  
  134.     empty = FALSE
  135.  
  136.     PARSE VALUE getarg(packet) WITH com format fullname
  137.  
  138.     CALL REPLY(packet, 0)
  139.  
  140.     IF (com = 'compile') THEN DO
  141.  
  142.     /* and now, Server-to-front */
  143.     ADDRESS COMMAND PToFront PubScreen
  144.     /* Result: 5: not OS 2.0, 10: PubScreen not found,
  145.         15: no Intuition.library. */
  146.  
  147.     IF '&' ~= left(format,1) THEN format = '&'||format
  148.     ELSE IF '&' = format THEN format = "" /* use virtex default format */
  149.  
  150.     fullname = STRIP(fullname)    /* beware of blanks (eg RAM DISK:) */
  151.  
  152.     /* make RAM DISK: a special case */
  153.     IF "RAM DISK:" = UPPER(LEFT(fullname,9)) THEN
  154.          fullname="RAM:"||SUBSTR(fullname,10)
  155.  
  156. /* KLUDGE: Do NOT accept names with blanks, I could do a better job here */
  157.     IF 0 ~= INDEX(fullname," ") THEN
  158.        say "- Sorry, no spaces in filenames :"fullname
  159.     ELSE DO
  160.  
  161. /**     namestruc fullname
  162.      if 0 ~= RC THEN DO
  163.         say "- Function "namestruc" not found, aborted!"
  164.         SIGNAL finish
  165.         END
  166.      PARSE VALUE RESULT WITH ivol idir iname .
  167. **/
  168.      PARSE VALUE namestruc(fullname) WITH ivol idir iname .
  169.  
  170.      texdir = LEFT(fullname, ivol+idir)  /*(TeX:doc/sample.tex-> TeX:doc/)*/
  171.      nameonly = SUBSTR(fullname, 1+ivol+idir)        /*(sample.tex)*/
  172.      baseonly = SUBSTR(fullname, 1+ivol+idir, iname)    /*(sample)*/
  173.      fullbase = LEFT(fullname, ivol+idir+iname)    /*(TeX:doc/sample)*/
  174.  
  175.      IF 0=ivol THEN DO
  176.        say "- An absolute path must be used (eg. <device|volumename>:...) !"
  177.        END
  178.      ELSE IF (SUBSTR(fullname, 1+ivol+idir+iname,1) ~= ".") THEN DO
  179.        say "- Sorry, filename must have a .<extension> :"fullname
  180.        END
  181.      ELSE DO
  182.  
  183.       IF ~EXISTS(fullname) THEN DO
  184.        say "- Couldn't locate "fullname" ! Missing a path ?"
  185.        END
  186.       ELSE DO
  187.  
  188.        CALL DELETE(fullbase||".log")
  189.        /* I feel there's no need to delete the .dvi file. */
  190.  
  191.        virtex = GETCLIP("virtex")    /* get TeX or bigTeX */
  192.        IF "" = virtex THEN virtex = 'virtex'
  193.  
  194.        say '- Start command: 'virtex format nameonly
  195.        say '   in directory 'texdir
  196.        CALL PRAGMA('directory',texdir)
  197.  
  198.        CALL SETCLIP("TEXFILE",fullname)
  199.        now = TIME('s')
  200.        CALL SETCLIP("TEXTIME",now)
  201.  
  202. /* The "< *" is a hack, no ? */
  203.        ADDRESS COMMAND virtex' < * 'format nameonly
  204.  
  205. /* How can I get rid of the Rexx message "+++ Command returned >=65536"
  206.    with versions of ARexx <= V1.10 ? */
  207.        IF filter2 THEN errorcode = RC % 65536 /* ARexx V1.10    */
  208.        ELSE           errorcode = RC      /* ARexx V1.14, V1.15    */
  209.        IF errorcode == 0 THEN DO
  210.         say
  211.         say "- Compiled file "fullname" without errors."
  212.         IF (SHOW('P','showdvi')) THEN DO
  213.            ADDRESS "showdvi"
  214.            OPTIONS RESULTS
  215.            getfile
  216.            loadedfile = RESULT
  217.            getdir
  218.            loadeddir = RESULT
  219.            IF RIGHT(loadeddir,1)~=':' & RIGHT(loadeddir,1)~='/' THEN
  220.                loadeddir = loadeddir||'/'
  221.            IF texdir = loadeddir & 1 = INDEX(UPPER(loadedfile||".dvi"),UPPER(baseonly||".dvi")) THEN DO
  222.             say "- Updating file in ShowDVI."
  223.             loadagain
  224.             END
  225.            ELSE DO
  226.             say "- Loading file "fullbase".dvi into ShowDVI."
  227.             loadnew fullbase||".dvi"
  228.             END
  229.            tofront
  230.            END
  231.         ELSE DO
  232.            say "- ShowDVI is not running."
  233.            END
  234.         END
  235.        ELSE DO
  236.         say
  237.         say "- Compilation not successful, error level "errorcode"."
  238.         IF getclip("TEXTIME") == now & EXISTS(fullbase||".log") THEN DO
  239.           editscript = WORD(mygetenv("TEXREXXEDIT"),1)
  240.           IF "" = editscript THEN editscript = 'TeXedit.rexx'
  241.           say "- Calling editor for file "fullname" via "editscript"."
  242.           editscript fullname 0
  243.           END
  244.         END
  245.        END    /* exists */
  246.       END    /* !ivol, "." */
  247.      END    /* !index */
  248.     END    /* compile */
  249.     END        /* !NULL() */
  250.   END        /* do */
  251.  
  252. finish:
  253.  
  254. ADDRESS COMMAND PToFront PubScreen
  255. /* Result: 5: not OS 2.0, 10: PubScreen not found, 15: no Intuition.library */
  256.  
  257. say "- Closing down port "portname
  258. CALL CLOSEPORT portname
  259.  
  260. IF PubScreen ~= "Workbench" THEN
  261.     say "  Type CTRL-\ or hit the CLOSE Gadget to close the window."
  262.     /* das koennte man im Prinzip immer (unter 2.0) ausgeben */
  263.  
  264. SIGNAL cleanup
  265.  
  266. break_C:
  267. break_D:
  268. say
  269. say "- Break received."
  270.  
  271. cleanup:
  272. CALL SETCLIP("TEXFILE")
  273. CALL SETCLIP("TEXTIME")
  274. CALL SETCLIP("TEXQUERY")
  275. EXIT
  276.  
  277.  
  278. mygetenv: PROCEDURE    /* when will ARexx supply GetEnv/SetEnv ? */
  279.    PARSE ARG name
  280.  
  281.    IF open(TEMPFILE,"ENV:"||name,'r') THEN DO
  282.     gives = readln(TEMPFILE)
  283.     CALL close TEMPFILE
  284.     END
  285.    ELSE gives = ""
  286.  
  287.    RETURN gives
  288.  
  289. mysetenv: PROCEDURE
  290.    PARSE ARG name,content
  291.  
  292.    ADDRESS COMMAND "SetEnv" name content
  293.  
  294.    RETURN
  295.  
  296.